﻿using System;
using System.Reflection;
using System.Configuration;
namespace jutehui.DALFactory
{
    /// <summary>
    /// Abstract Factory pattern to create the DAL。
    /// 如果在这里创建对象报错，请检查web.config里是否修改了<add key="DAL" value="Maticsoft.SQLServerDAL" />。
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = "jutehui.SQLServerDAL";//ConfigurationManager.AppSettings["DAL"];        
        public DataAccess()
        { }

        #region CreateObject

        //不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch//(System.Exception ex)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }
        //使用缓存
        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                }
            }
            return objType;
        }
        #endregion

        #region 泛型生成
        ///// <summary>
        ///// 创建数据层接口。
        ///// </summary>
        //public static t Create(string ClassName)
        //{

        //    string ClassNamespace = AssemblyPath +"."+ ClassName;
        //    object objType = CreateObject(AssemblyPath, ClassNamespace);
        //    return (t)objType;
        //}
        #endregion

        #region CreateSysManage
        public static jutehui.IDAL.ISysManage CreateSysManage()
        {
            //方式1			
            //return (jutehui.IDAL.ISysManage)Assembly.Load(AssemblyPath).CreateInstance(AssemblyPath+".SysManage");

            //方式2 			
            string classNamespace = AssemblyPath + ".SysManage";
            object objType = CreateObject(AssemblyPath, classNamespace);
            return (jutehui.IDAL.ISysManage)objType;
        }
        #endregion



        /// <summary>
        /// 创建houseproducts数据层接口。
        /// </summary>
        public static jutehui.IDAL.Ihouseproducts Createhouseproducts()
        {

            string ClassNamespace = AssemblyPath + ".houseproducts";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Ihouseproducts)objType;
        }


        /// <summary>
        /// 创建navigationtype数据层接口。
        /// </summary>
        public static jutehui.IDAL.Inavigationtype Createnavigationtype()
        {

            string ClassNamespace = AssemblyPath + ".navigationtype";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Inavigationtype)objType;
        }


        /// <summary>
        /// 创建Product数据层接口。
        /// </summary>
        public static jutehui.IDAL.IProduct CreateProduct()
        {

            string ClassNamespace = AssemblyPath + ".Product";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IProduct)objType;
        }


        /// <summary>
        /// 创建productstype数据层接口。
        /// </summary>
        public static jutehui.IDAL.Iproductstype Createproductstype()
        {

            string ClassNamespace = AssemblyPath + ".productstype";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Iproductstype)objType;
        }


        /// <summary>
        /// 创建userinfo数据层接口。
        /// </summary>
        public static jutehui.IDAL.Iuserinfo Createuserinfo()
        {

            string ClassNamespace = AssemblyPath + ".userinfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Iuserinfo)objType;
        }
        /// <summary>
        /// 创建versioncontrol数据层接口。
        /// </summary>
        public static jutehui.IDAL.Iversioncontrol Createversioncontrol()
        {

            string ClassNamespace = AssemblyPath + ".versioncontrol";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Iversioncontrol)objType;
        }
        /// <summary>
        /// 创建feedback数据层接口。
        /// </summary>
        public static jutehui.IDAL.Ifeedback Createfeedback()
        {

            string ClassNamespace = AssemblyPath + ".feedback";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Ifeedback)objType;
        }
        /// <summary>
        /// 创建hpinfos数据层接口。
        /// </summary>
        public static jutehui.IDAL.Ihpinfos Createhpinfos()
        {

            string ClassNamespace = AssemblyPath + ".hpinfos";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Ihpinfos)objType;
        }
        /// <summary>
        /// 创建clicktimes数据层接口。
        /// </summary>
        public static jutehui.IDAL.Iclicktimes Createclicktimes()
        {

            string ClassNamespace = AssemblyPath + ".clicktimes";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Iclicktimes)objType;
        }
        /// <summary>
        /// 创建hotproducts数据层接口。
        /// </summary>
        public static jutehui.IDAL.Ihotproducts Createhotproducts()
        {

            string ClassNamespace = AssemblyPath + ".hotproducts";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Ihotproducts)objType;
        }
        /// <summary>
        /// 创建imgSize数据层接口。
        /// </summary>
        public static jutehui.IDAL.IimgSize CreateimgSize()
        {

            string ClassNamespace = AssemblyPath + ".imgSize";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IimgSize)objType;
        }


        /// <summary>
        /// 创建jthimg数据层接口。
        /// </summary>
        public static jutehui.IDAL.Ijthimg Createjthimg()
        {

            string ClassNamespace = AssemblyPath + ".jthimg";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.Ijthimg)objType;
        }
        /// <summary>
        /// 创建webClass数据层接口。
        /// </summary>
        public static jutehui.IDAL.IwebClass CreatewebClass()
        {

            string ClassNamespace = AssemblyPath + ".webClass";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IwebClass)objType;
        }
        /// <summary>
        /// 创建imginfoView数据层接口。
        /// </summary>
        public static jutehui.IDAL.IimginfoView CreateimginfoView()
        {

            string ClassNamespace = AssemblyPath + ".imginfoView";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IimginfoView)objType;
        }

        /// <summary>
        /// 创建userVersion数据层接口。
        /// </summary>
        public static jutehui.IDAL.IuserVersion CreateuserVersion()
        {

            string ClassNamespace = AssemblyPath + ".userVersion";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IuserVersion)objType;
        }
        /// <summary>
        /// 创建secondKillProducts数据层接口。
        /// </summary>
        public static jutehui.IDAL.IsecondKillProducts CreatesecondKillProducts()
        {

            string ClassNamespace = AssemblyPath + ".secondKillProducts";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IsecondKillProducts)objType;
        }
        /// <summary>
        /// 创建ProducerTimes数据层接口。
        /// </summary>
        public static jutehui.IDAL.IProducerTimes CreateProducerTimes()
        {

            string ClassNamespace = AssemblyPath + ".ProducerTimes";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IProducerTimes)objType;
        }
        /// <summary>
        /// 创建userMaininfo数据层接口。
        /// </summary>
        public static jutehui.IDAL.IuserMaininfo CreateuserMaininfo()
        {

            string ClassNamespace = AssemblyPath + ".userMaininfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IuserMaininfo)objType;
        }
        /// <summary>
        /// 创建userDetailInfo数据层接口。
        /// </summary>
        public static jutehui.IDAL.IuserDetailInfo CreateuserDetailInfo()
        {

            string ClassNamespace = AssemblyPath + ".userDetailInfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IuserDetailInfo)objType;
        }
        /// <summary>
        /// 创建userOrder数据层接口。
        /// </summary>
        public static jutehui.IDAL.IuserOrder CreateuserOrder()
        {

            string ClassNamespace = AssemblyPath + ".userOrder";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IuserOrder)objType;
        }
        /// <summary>
        /// 创建userShare数据层接口。
        /// </summary>
        public static jutehui.IDAL.IuserShare CreateuserShare()
        {

            string ClassNamespace = AssemblyPath + ".userShare";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IuserShare)objType;
        }
        /// <summary>
        /// 创建userSign数据层接口。
        /// </summary>
        public static jutehui.IDAL.IuserSign CreateuserSign()
        {

            string ClassNamespace = AssemblyPath + ".userSign";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IuserSign)objType;
        }
        /// <summary>
        /// 创建UserInfosView数据层接口。
        /// </summary>
        public static jutehui.IDAL.IUserInfosView CreateUserInfosView()
        {

            string ClassNamespace = AssemblyPath + ".UserInfosView";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jutehui.IDAL.IUserInfosView)objType;
        }


    }
}